﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using API;
using DB;

namespace ProductManage
{
    public partial class FormEbayProducts : Form
    {
        private EbayGetSellerListApi _ebayGetSellerListApi;
        private EbayGetSellerListApi EbayGetSellerListApi
        {
            get
            {
                if (_ebayGetSellerListApi == null)
                    _ebayGetSellerListApi = new EbayGetSellerListApi(EBaySellers.QqSerller);
                return _ebayGetSellerListApi;
            }
        }

        private EbayAddItemApi _ebayAddItemApi;
        private EbayAddItemApi EbayAddItemApi
        {
            get
            {
                if (_ebayAddItemApi == null)
                    _ebayAddItemApi = new EbayAddItemApi(EBaySellers.QqSerller);
                return _ebayAddItemApi;
            }
        }
        public FormEbayProducts()
        {
            InitializeComponent();
            this.Type.Items.AddRange("", "Charms", "Clips", "Pendants", "Spacers", "SafetyChains", "SnakeChain");
            this.Stones.Items.AddRange("", "With Stone", "Without Stone", "Pavé");
            this.Metals.Items.AddRange("", "Silver", "Gold", "Two Tone");
            this.Themes.Items.AddRange("", "Alphabet & Numbers", "Animals", "Cause & Awareness", "Decorative", "Fairy Tale",
                "Family and Friends", "Floral", "Glamour", "Glass", "Hobbies", "Love", "Nature", "Occasions", "Occupation", "Symbols", "Travel");
            this.Materials.Items.AddRange("", "Sterling Silver", "Enamel", "Glass", "Pearl");
        }

        #region display
        private void btnLoadOnlineData_Click(object sender, EventArgs e)
        {
            var v= EbayGetSellerListApi.GetOnlineItemList();
            labelOnlineData.Text = "Count:" + v.Count;
        }

        private ProductsDb _db = ProductsDb.DbMysql;
        private List<Product> listProduct;
        private List<string> listAttributr = new List<string>(){"Type", "Size", "Weight", "Color", 
            "Themes","Stones", "Metals",  "Materials", "Price", "PriceSale", "PricePurchase",}; 
        private void buttonLoadData_Click(object sender, EventArgs e)
        {
            listProduct = _db.GetAllProducts();
            this.textBoxPageIndex.Text = "0";
        }

        private List<Product> GetProductListBySearch(List<Product> list, string strKeywords)
        {
            strKeywords = strKeywords.Trim(' ', '\r', '\n', ',');
            if (strKeywords == "") return list;
            var listRtn = new List<Product>();

            var keywords = strKeywords.Split(',', ' ', '\r', '\n');
            if (keywords.Count() == 1)
                listRtn = (from p in list where p.SKU != null && p.SKU.Contains(keywords[0]) select p).ToList();
            else
                listRtn = (from p in list where p.SKU != null && keywords.Contains(p.SKU) select p).ToList();
            var listType = (from p in list where p.Type!= null && p.Type.Contains(strKeywords) select p).ToList();
            var listTheme = (from p in list where p.Theme != null && p.Theme.Contains(strKeywords) select p).ToList();
            var listStone = (from p in list where p.Stone != null && p.Stone.Contains(strKeywords) select p).ToList();
            var listMetals = (from p in list where p.Metals != null && p.Metals.Contains(strKeywords) select p).ToList();
            var listMaterial = (from p in list where p.Material != null && p.Material.Contains(strKeywords) select p).ToList();
            var listFeature = (from p in list where p.Feature != null && p.Feature.Contains(strKeywords) select p).ToList();
            listRtn.AddRange(listType);
            listRtn.AddRange(listTheme);
            listRtn.AddRange(listStone);
            listRtn.AddRange(listMetals);
            listRtn.AddRange(listMaterial);
            listRtn.AddRange(listFeature);
            return listRtn;
        }
        private void ShowProduct(int page, int pageTotal)
        {
            if (listProduct == null) listProduct = _db.GetAllProducts();
            var list = GetProductListBySearch(listProduct, this.textBoxSearch.Text).Skip(pageTotal * page).Take(pageTotal).ToList();
            dataGridViewProductList.Rows.Clear();
            this.imageList1.Images.Clear();
            foreach (var p in list)
            {
                Image image = new Bitmap(1, 1);
                var images = ProductImageApi.GetImages(p.SKU);
                if (images.Count() != 0)
                    image = images[0];
                this.imageList1.Images.Add(p.SKU, image);
                var i = dataGridViewProductList.Rows.Add(new object[] { false,this.imageList1.Images[p.SKU], p.SKU,p.Type,p.Size,p.Weight,p.Color,p.Theme
                    ,p.Stone,p.Metals,p.Material,p.Price,p.PriceSale,p.PricePurchase,p.Feature });
                dataGridViewProductList.Rows[i].Tag = p;
            }
        }

        private void buttonPre_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(this.textBoxPageIndex.Text) - 1;
            if (i < 0) i = 0;
            this.textBoxPageIndex.Text = i.ToString();
        }

        private void buttonNex_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(this.textBoxPageIndex.Text) + 1;
            if (i > listProduct.Count()) i = listProduct.Count();
            this.textBoxPageIndex.Text = i.ToString();
        }

        private void textBoxPageIndex_TextChanged(object sender, EventArgs e)
        {
            ShowProduct(Convert.ToInt32(this.textBoxPageIndex.Text), Convert.ToInt32(this.textBoxPageTotal.Text));
        }

        private void textBoxPageTotal_TextChanged(object sender, EventArgs e)
        {
            ShowProduct(Convert.ToInt32(this.textBoxPageIndex.Text), Convert.ToInt32(this.textBoxPageTotal.Text));
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            ShowProduct(Convert.ToInt32(this.textBoxPageIndex.Text), Convert.ToInt32(this.textBoxPageTotal.Text));
        }

        #region picture
        private Dictionary<string, Image> dicImage = new Dictionary<string, Image>();
        private void dataGridViewProductList_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewProductList.SelectedRows.Count > 0)
                ShowProductPics(this.dataGridViewProductList.SelectedRows[0].Tag as Product);
        }
        private void ShowProductPics(Product product)
        {
            this.dicImage.Clear();
            this.imageList1.Images.Clear();
            this.listViewPics.Items.Clear();
            var images = ProductImageApi.GetImages(product.SKU);
            int i = 1;
            foreach (var image in images)
            {
                string key = "pic" + i;
                this.dicImage.Add(key, image);
                this.imageList1.Images.Add(key, image);
                this.listViewPics.Items.Add(key, key);
                i++;
            }
            if (dicImage.Count > 0)
                this.pictureBoxPic.Image = dicImage["pic1"];
            else
                this.pictureBoxPic.Image = null;
        }

        private void listViewPics_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewPics.SelectedItems.Count > 0)
                this.pictureBoxPic.Image = dicImage[this.listViewPics.SelectedItems[0].ImageKey];
        }
        #endregion
        #endregion

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewProductList.SelectedRows.Count > 0)
                EbayAddItemApi.AddItem(this.dataGridViewProductList.SelectedRows[0].Tag as Product);
        }

    }
}
